'''
Created on Mar 20, 2013

@author: jason.richmond
This class will act as the server that processes the packets in the job queue
'''

from SimPy.Simulation import *
from Globals import *

class Server(Process):
    Idle = None
    PriorityQueue = [] #priority job queue
    Queue = [] #best effort queue
    SrvRate = None
        
    def __init__(self):
        Process.__init__(self)
        Server.Idle = True;
        
    def Run(self):
        while 1:
            #The server will sleep until it is awakened
            yield passivate,self
            
            #Once it the server is awake it will not be idle anymore
            Server.Idle = False
            
            #It will then process a job with a link utilization of 80%
            #Get a random number between 0 and 1.
            utilize = G.Rnd.random()
            
            if utilize <= .8:
                #process a job
                print 'processing job'
                
                #get a job determine how often you will pull the job from
                #the priority queue.
                getPriority = G.Rnd.random()
                
                if getPriority <= .8:
                    #process job from priority queue
                    J = Server.PriorityQueue.pop(0)
                else:
                    #process job from the best effort queue
                    J = Server.Queue.pop(0)
                    
                #a random amount of time will pass and the job will be processed.
                yield hold,self,G.Rnd.expovariate(Server.SrvRate)
            else:
                #the wait time will be proportional to the Bandwidth available (10 Mbps)
                #on the link.
                print 'yielding'
                yield hold,self,G.Rnd.expovariate(Machine.SrvRate)
                
            Server.Idle = True
            
            
